<?php 


function afficheComplexe($var){
	echo"<pre>";
	print_r($var);
	echo"</pre>";	
}
function affichePost(){
	foreach($_POST as $key => $val) echo '$_POST["'.$key.'"]='.$val.'<br />';
}
//retourne le nom du fichier
function titre(){
	$titre='';
	$nom=$_SERVER ['SCRIPT_NAME'];
	//echo 'SCRIPT_NAME'.$nom.'<br/>';
	$titre= rtrim(substr($nom,strripos ($nom,'/')+1),'.php');	
	return $titre;
}


function Entete($title = 'titre', $style = "stylepage") {

	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n";
	echo "\"http://www.w3.org/TR/html4/loose.dtd\">\n";
	echo "<html>\n";
	echo "<head>\n";
	echo "<title>$title</title>\n";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n";
	echo "<link rel=\"stylesheet\" media=\"screen\" type=\"text/css\" title=\"$style\" href=\"$style.css\" /> \n";
	echo "<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"../images/oiseau.png\" />";
	echo "<script src=\"jquery.js \" type=\"text/JavaScript\" ></script>";
	echo "</head>\n";
	echo "<body>\n";	
}
function PiedPage() {

	echo "\n<div id=\"footer\"><p>Copyright \"@Renays  Corporation\" 2012, tous droits r&#233;serv&#233;s ||</p>"; 
	echo "<p>Partenaires ||</p><p> Mentions l�gales  </p>||</p><p> Contacts.</p></div>\n";
}
function heure(){
	$heure = date("H");
			$minute = date("i");	
			echo "Nous sommes le ".date('d/m/Y')."<br/> il est $heure h $minute";
}

function img($adresse,$image_taille){
	ConnexionDB();
	$sql="SELECT `titre_img`, `position_img`, `nom_img`, `commentaire_img`, `width`, `height` FROM `photo_akf` WHERE `nom_img` LIKE '$adresse%' ORDER BY (`position_img`) ASC;";
	$reponse = mysql_query($sql);
	while ($donnees = mysql_fetch_array($reponse)) {
			$titre_img=$donnees['titre_img'];
			$nom_img=$donnees['nom_img'];
			$height=$donnees['height'];
			$width=$donnees['width'];
			$commentaire_img=$donnees['commentaire_img'];
			if ($image_taille=='small'){
				$image_small=str_replace("large","small",$nom_img);
				 echo '<a style="height: 55px; width: 55px;"  href="#'.$titre_img.'"  id="'.$titre_img.'_small">';
				 echo '<img title="'.$titre_img.'" style="height: 55px; width: 55px;" src="upload/'.$image_small.'"  /></a>';
			}else {
				 	echo '<img src="upload/'.$nom_img.'"id="'.$titre_img.'" width="'.$width.'" height="'.$height.'" title="'.$titre_img.'" alt="'.$commentaire_img.'" />';
//				 	echo '<div><p>'.$donnees['commentaire_img'].'</p></div>';
				 }
	}
	
	DeconnexionDB();	
}

function ConnexionDB() {
//	$connection = mysql_connect("sql.free.fr","renays2012","renays2012");// Connexion � MySQL free
	$connection = mysql_connect("localhost","root","renays2012");// Connexion � MySQL local
	if (!$connection) {
		echo "connexion impossible la base";
	}else{
		mysql_select_db('renays2012',$connection);
	}	
}
function DeconnexionDB() {
	mysql_close(); // D�connexion de la base
}
function isAuthentificationOK($ident, $mdp) {
	$authentification = false;
	ConnexionDB();
	$reponse = mysql_query("SELECT * FROM user_akf "); // Requ�te SQL	
	while ($donnees = mysql_fetch_array($reponse)) {
		if ($donnees['user'] == $ident && $donnees['password'] == $mdp) {
			$authentification = true;
//			$_SESSION['nom'] = $donnees['user'];			
		}
	}
	DeconnexionDB();
	return $authentification;
}
function creePhoto($titre_img,$position_img,$nom_img,$commentaire_img,$width,$height,$weight){
//	ConnexionDB();
	$sql="INSERT INTO photo_akf VALUES ('', '$titre_img', '$position_img', '$nom_img','$commentaire_img', '$width', '$height', '$weight');";
	$reponse = mysql_query($sql);
//	DeconnexionDB();
}
function suppPhoto(){
	 $images=$_POST['ressource'];
	ConnexionDB();
	foreach ($images as $image){
        $image_small=str_replace("large","small",$image);
          
		if (file_exists($image) && file_exists($image_small)){
			unlink($image);
			unlink($image_small);
		}
    	$sql="DELETE FROM photo_akf WHERE `nom_img` = '$image';";
		$reponse = mysql_query($sql);
    }
	DeconnexionDB();
}
function modifPhoto(){
	$images=$_POST['ressource'];
	ConnexionDB();
	foreach ($images as $image){
		$no=str_replace(".","_",$image);
        $titre_img=$_POST[$no.'_titre'];
        $position_img=$_POST[$no.'_pos'];
        $commentaire_img=$_POST[$no.'_com'];
    	$sql="UPDATE photo_akf SET `titre_img`='$titre_img', `position_img`='$position_img', `commentaire_img`='$commentaire_img' WHERE `nom_img` = '$image';";
		$reponse = mysql_query($sql);
    }
	DeconnexionDB();
}
$c=0;
function afficherPhotos($menu){
	ConnexionDB();
	$sql="SELECT `id`, `titre_img`, `position_img`, `nom_img`, `commentaire_img`, `width`, `height`, `weight` FROM `photo_akf` WHERE `nom_img` LIKE '$menu%' ORDER BY (`position_img`) ASC LIMIT 0 , 100;";
	$reponse = mysql_query($sql);
	echo '<table id="table">';
	 global $c;
	while ($donnees = mysql_fetch_array($reponse)) {
		$c++;
		$nom=$donnees['nom_img'];
		 echo '<tr id="tr'.$c.'">';
		 echo '<td><span id="'.$c.'"><input type="checkbox" name="ressource[]" value="'.$nom.'"></span>';
		 echo '<a href="'.$nom.'" target="blank">'; 
		 echo '<img src="'.$nom.'"title="'.$donnees['titre_img'].'" style="" /></a></td>';
		 echo '<td class="right">';
		 echo 'Titre: <input type="text" readonly="readonly" size="18" name="'.$nom.'_titre" value="'.$donnees['titre_img'].'" id="tr'.$c.'_titre"> <br/> ';
		 echo 'N&#176;: <input type="number" readonly="readonly" size="3" name="'.$nom.'_pos" value="'.$donnees['position_img'].'" id="tr'.$c.'_pos"><br/><br/>';
		 echo 'L&#233;gende: <textarea rows="1" cols="25" readonly="readonly" name="'.$nom.'_com" id="tr'.$c.'_com" >'.$donnees['commentaire_img'].'</textarea><br/>';
		 echo 'Nom: '.$donnees['nom_img'].'<br/>';
		 echo 'Dimensions: '.$donnees['width'].'x'.$donnees['height'].'px, Poids: '.$donnees['weight'].'Ko<br/>';
		 
		 echo '</tr>';
	}
	echo '</table>';
	
	DeconnexionDB();
}


function resizeImg($oldname,$newname){

	$newh = 80;
	
	// interpolating
	
	$size = getImageSize($oldname);
	$w = $size[0];
	$h = $size[1];
	$neww = intval($newh * $w / $h);
	
	
	// converting
	
	$resimage = imagecreatefromjpeg($oldname); 
	$newimage = imagecreatetruecolor($neww, $newh);  // use alternate function if not installed
	imageCopyResampled($newimage, $resimage,0,0,0,0,$neww, $newh, $w, $h);

	// saving
	
	imageJpeg($newimage, $newname, 85);
	
}
?>
